#include <stdio.h>
#include "serial.h"
#include <windows.h>
#include "dlt698_45.h"
#include "common.h"

unsigned char msg[] = {
0x02, 0x03, 
0x02, 0x06, 
0x0a, 0x1c, 0x4e, 0x33, 0x32, 0x5f, 0x52, 0x54, 0x4f, 0x53, 0x5f, 0x57, 0x4b, 0x5f, 0x32, 0x30, 0x32, 0x35, 0x30, 0x31, 0x32, 0x30, 0x31, 0x35, 0x32, 0x39, 0x2e, 0x62, 0x69, 0x6e, 
0x0a, 0x11, 0x2f, 0x6d, 0x6e, 0x74, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x63, 0x6c, 0x6f, 0x75, 0x2e, 0x74, 0x67, 0x7a, 
0x06, 0x00, 0x01, 0x9b, 0x2c, 
0x04, 0x03, 0x07, 
0x0a, 0x04, 0x76, 0x31, 0x2e, 0x32, 
0x16, 0x00, 
0x12, 0x02, 0x00, 
0x02, 0x02, 
0x16, 0x00, 
0x09, 0x00
};

unsigned short point[20] = {0};
unsigned char recvbuf[1024] = {0};
unsigned char sendbuf[1024] = {0};

typedef	struct
{
	unsigned short	addr	: 12;
	unsigned short	cmd		: 4;
}Cmd_Addr_Format_SingleMode_t;

Cmd_Addr_Format_SingleMode_t a; 

int main(int argc, char** argv) 
{
	PORT COM1;
	
    unsigned short send_len = 0;
    unsigned short recv_len = 0;
    int ret;
    int i;
    unsigned char k = 1;
    
    int pos = 0;
	unsigned short idx = 0;
	
//	unsigned short len;
//	
//	
//	len = GetMsgApduDataNum(msg, point, &idx);
//	
//	for(i = 0; i < idx; i++)
//	{
//		printf("%d \n", point[i]);
//	}
//	
//	printf("%d \n", len);
//	printf("%d \n", sizeof(msg));
//	return 0;
//	idx = GetMsgApduDataIdx(msg, point);
//	
//	
//	for(i = 0; i < idx; i++)
//	{
//		printf("%d \n", point[i]);
//	}
    
//    unsigned char buf[4];
//    FloatToBigEndianShort(3.1415, 3,buf);
//    
//    printf("%02x ", buf[0]);
//    printf("%02x ", buf[1]);
//    printf("%02x ", buf[2]);
//    printf("%02x ", buf[3]);

//	a.cmd = k;
//	printf("%02x ", a.cmd);
    
    COM1 = serial_init(1, 9600, 8, 1, 1);
    		
	Dlt69845AppInit();
    while(1)
    {          
        recv_len = Serial_ReciveData(COM1, recvbuf, 1024);        
        if(recv_len)
        {
//		    for(i = 0; i < recv_len; i++)
//			{
//				printf("%02x ", recvbuf[i]);
//			}
        	ret = Dlt69845Handle(recvbuf, recv_len, &sendbuf[0], &send_len);
        	
        	if(ret >= 0)
        	{
//        		memset(sendbuf, 0xfe,4);
//        		send_len += 4;
        		Serial_SendData(COM1, sendbuf, send_len);
			}
        		

		}    
    }    

	return 0;
}



